#include <cstdio>
#include <cstring>
int main()
{//要点：理解题意，规律总结
    char str[101];
    int a, b, c;
    int N;
    int pnum, tnum;//记录当前P的个数
    int len;//记录当前遍历的字符串的长度
    int i;
    scanf("%d", &N);
    while(N--)
    {
        scanf("%s", str); //读取字符串的时候，会自动在字符串末尾添加结束标志'\0'
        pnum = tnum = 0;
        a = b = c = 0;
        len = strlen(str);
        for(i = 0; i < len; ++i)
        {
            if(str[i] == 'A'){
                if(0 == pnum) ++a;
                else if(1 == tnum) ++c;
                else if(1 == pnum) ++b;
            }else if(str[i] == 'P'){
                if(++pnum == 2) break;
            }else if(str[i] == 'T'){
                if(0 == pnum) break;//此条件保证P在T之前出现
                else if(++tnum == 2) break;
            }
            else break;//有其他字符出现，立即跳出循环
        }   
        //printf("a = %d, b = %d, c = %d, pbnum = %d\n", a, b, c, pnum);
        if(i != len || pnum != 1 || tnum != 1 || b == 0 || c != b * a) printf ("NO\n");
        else printf("YES\n");
    }
}